<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Services\Form\initFormData as InitFormData;
use Cache;
use Illuminate\Http\Request;

class Market extends Controller
{
    use \App\Traits\MarketTrait;
    const CEIL_CHAIN_CODE = 'schz_gylhz';
    const CEIL_ASSET_CODE = 'schz_lsjrhz';
    const CEIL_TECH_CODE = 'schz_xjshz';
    const CEIL_BANNER_CODE = 'schz_banner';
    const CEIL_GREEN_COO = 'schz_tzfzhz';
    const CEIL_FIND_SERVICER = 'schz_zfws';

    public $request;
    public $formData;
    public $breadMenuList;
    use \App\Traits\ApiTrait;
    public $curl;

    public function __construct(Request $request, InitFormData $formData)
    {
        $this->request = $request;
        $this->formData = $formData;
        $this->breadMenuList = config('front.bread_menu');
        $this->curl = new \Ixudra\Curl\CurlService();
    }

    // public function index()
    // {
    //     if (env('CACHE_INDEX') && Cache::has('market_coo_index_view_data')) {
    //         $viewData = Cache::get('market_coo_index_view_data');
    //         return view('web.market.index', $viewData);
    //     }
    //
    //     //$projectData = $this->getProjectAdList(pareng::CODE_ASSET_FIND_PROJECT);
    //     //$chainData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_CHAIN_CODE);
    //     //$techData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_TECH_CODE);
    //
    //     $greenCooData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_GREEN_COO);
    //     $findServicerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_FIND_SERVICER);
    //     $assetData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_ASSET_CODE);
    //     $needsData = $this->getNeedsList(parent::CODE_MARKET_NEEDS);
    //
    //     $bannerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_BANNER_CODE);
    //     $viewData = [
    //         'seo' => $this->returnSeoInfo('market_index'),
    //         'selectedMenu' => parent::MENU_MARKET_COO,
    //
    //         //'project_list' => $projectData,
    //         //'chain_list' => $chainData,
    //         //'tech_list' => $techData,
    //         'green_coo_list' => $greenCooData,
    //         'find_servicer_list' => $findServicerData,
    //         'asset_list' => $assetData,
    //         'needs_list' => $needsData,
    //         'banner_info' => $bannerData,
    //     ];
    //     Cache::forever('market_coo_index_view_data', $viewData);
    //     return view('web.market.index', $viewData);
    // }
    /*
    */
    public function index(Request $request)
    {
        $idArr=[
          '1'=>'20/24',
          '2'=>'20/22',
          '3'=>'20/23',
          '4'=>'20/21',
        ];
        if(isset($_REQUEST['id']) && $_REQUEST['id']>0 && $_REQUEST['id']<=4)
        $id=$_REQUEST['id'];

        else {
          $id=1;
        }
        $categoryId=$idArr["$id"];
        // print_r($categoryId);
        $list=$this->ajaxArticleList($categoryId);
        // var_dump($list);
        if($list['code']!=-1){
          $listData=$list['data']['records'];
          // print_r($listData);
        }
        else $listData=[];
        $greenCooData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_GREEN_COO);
        $findServicerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_FIND_SERVICER);
        $assetData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_ASSET_CODE);
        $needsData = $this->getNeedsList(parent::CODE_MARKET_NEEDS);

        $bannerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_BANNER_CODE);
        $viewData = [
            'seo' => $this->returnSeoInfo('market_index'),
            'selectedMenu' => parent::MENU_MARKET_COO,
            'listData'=>$listData,
            'green_coo_list' => $greenCooData,
            'find_servicer_list' => $findServicerData,
            'asset_list' => $assetData,
            'needs_list' => $needsData,
            'banner_info' => $bannerData,
            'id'=>$id,
        ];
        switch ($id) {
          case '1':
            return view('web.market.index', $viewData);
            break;
          case '2':
            return view('web.market.index2', $viewData);
            break;
          case '3':
            return view('web.market.index3', $viewData);
            break;
          case '4':
            return view('web.market.index4', $viewData);
            break;
          default:
            return view('web.market.index', $viewData);
            break;
        }

    }

    public function indexMobile(Request $request){
      $idArr=[
        '1'=>'20',
        '2'=>'20/22',
        '3'=>'20/23',
        '4'=>'20/21',
        '5'=>'54',
        '6'=>'58',
      ];
      if(isset($_REQUEST['cid']) && $_REQUEST['cid']>0 && $_REQUEST['cid']<=6)
      $id=$_REQUEST['cid'];

      else {
        $id=1;
      }
      $categoryId=$idArr["$id"];
      // print_r($categoryId);
      $list=$this->ajaxArticleList($categoryId);
      // var_dump($list);
      if($list['code']!=-1){
        $listData=$list['data']['records'];
        // print_r($listData);
      }
      else $listData=[];
      $greenCooData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_GREEN_COO);
      $findServicerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_FIND_SERVICER);
      $assetData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_ASSET_CODE);
      $needsData = $this->getNeedsList(parent::CODE_MARKET_NEEDS);

      $bannerData = $this->cacheCeilData(parent::CEIL_API_ID, self::CEIL_BANNER_CODE);
      $viewData = [
          'seo' => $this->returnSeoInfo('m_market'),
          'selectedMenu' => parent::MENU_MARKET_COO,
          'listData'=>$listData,
          'green_coo_list' => $greenCooData,
          'find_servicer_list' => $findServicerData,
          'asset_list' => $assetData,
          'needs_list' => $needsData,
          'banner_info' => $bannerData,
          'id'=>$id,
          'cid'=>$id,
      ];
      switch ($id) {
        case '1':
          return view('mobile.market.index1', $viewData);
          break;
        case '2':
          return view('mobile.market.index2', $viewData);
          break;
        case '3':
          return view('mobile.market.index3', $viewData);
          break;
        case '4':
          return view('mobile.market.index4', $viewData);
          break;
        case '6':
          return view('mobile.market.index_link', $viewData);
          break;
        default:
          return view('mobile.market.index1', $viewData);
          break;
      }
    }

    public function ajaxArticleList($id){
      $pageNumber=1;
      $pageSize=3;
      $categoryId=$id;
      // $pageNumber=$this->msgPageNum;
      //验证是否有cookie信息
        $rqUrl='http://api.crm.tanwin.cn/article/findByProgramaId';
        $rqData=['article_programa_id'=>$categoryId,'pageNumber'=>$pageNumber,'pageSize'=>$pageSize];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data'];
              $data['code']=1;
              $data['data']=$rData;
              return $data;
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return $data;
          }
    }

    public function jsonArticleList(Request $request){
      $pageNumber=$this->request['pageNum'];
      if(!$pageNumber || $pageNumber==null)$pageNumber=1;
      $pageSize=3;
      $categoryId='20';
      // $pageNumber=$this->msgPageNum;
      //验证是否有cookie信息
        $rqUrl='http://api.crm.tanwin.cn/article/findByProgramaId';
        $rqData=['article_programa_id'=>$categoryId,'pageNumber'=>$pageNumber,'pageSize'=>$pageSize];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data'];
              $data['code']=1;
              $data['data']=$rData;
              return response(json_encode($data));
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return response(json_encode($data));
          }
    }
    public function info(Request $request){
      if(isset($_REQUEST['id']) && $_REQUEST['id']>0 && $_REQUEST['id']<=7)
      $id=$_REQUEST['id'];

      else {
        $id=1;
      }
      $tempArr=['','web.market.ceil.peiXun.hehuo','web.market.ceil.waiKuo.duiHua','web.market.ceil.waiKuo.liangXiang','web.market.ceil.waiKuo.litui','web.market.ceil.waiKuo.rongYing','web.market.ceil.yanTao.lvSe','web.market.ceil.yanTao.yangShi'];
      $viewData = [
          'seo' => $this->returnSeoInfo('market_index'),
          'selectedMenu' => parent::MENU_MARKET_COO,
          'id'=>$id,];
      return view($tempArr[$id],$viewData);
    }

    public function video(Request $request){
      if(isset($_REQUEST['id']) && $_REQUEST['id']>0 && $_REQUEST['id']<=7)
      $id=$_REQUEST['id'];

      else {
        $id=1;
      }
      $viewData = [
          'seo' => $this->returnSeoInfo('market_index'),
          'selectedMenu' => parent::MENU_MARKET_COO,
          'id'=>$id,];
      return view('web.market.ceil.video.v'.$id,$viewData);
    }

    public function videoMobile(Request $request){
      if(isset($_REQUEST['id']) && $_REQUEST['id']>0 && $_REQUEST['id']<=7)
      $id=$_REQUEST['id'];

      else {
        $id=1;
      }
      $viewData = [
          'seo' => $this->returnSeoInfo('m_video'),
          'selectedMenu' => parent::MENU_MARKET_COO,
          'id'=>$id,];
      return view('mobile.market.ceil.video.v'.$id,$viewData);
    }
    /**
     * 技术合作内容列表
     * @return [type]
     */
    public function tech()
    {
        $result = $this->formData->getFilterMarketTechList($this->request, parent::CODE_ARTICLE_LIST);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_MARKET_COO, ['type' => parent::TAB_MENU_MARKET_COO_TECH]),
            ];
            return view('web.market.list.tech', $viewData);
        } else {
            $viewData = [
                'seo' => $this->returnSeoInfo(parent::TAB_MENU_MARKET_COO_TECH),
                'selectedMenu' => parent::MENU_MARKET_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO, parent::TAB_MENU_MARKET_COO_TECH],

                'dataList' => $dataList,
                'filter_area' => parent::TAB_MENU_MARKET_COO_TECH,

                'infoUrl' => $this->returnInfoUrl(parent::MENU_MARKET_COO, ['type' => parent::TAB_MENU_MARKET_COO_TECH]),
                'pageCount' => $pageCount,

            ];
            return view('web.market.tech', $viewData);
        }
    }

    public function htmlPage()
    {
        $pageInfo = [1, 2, 3, 4]; //此ID 与 blade,seo,front中的配置有关
        $id = $this->request->input('id') ? $this->request->input('id') : 1;
        if (!in_array($id, $pageInfo)) {
            abort(404);
            exit;
        }
        $viewData = [
            'seo' => $this->returnSeoInfo('seo_market_page_' . $id),
            'selectedMenu' => parent::MENU_MARKET_COO,

            'breadMenuList' => $this->breadMenuList,
            'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO, 'market_page_' . $id],

        ];
        return view('web.market.html_page.' . $id, $viewData);
    }

    public function carbonCity()
    {
        $viewData = [
            'seo' => $this->returnSeoInfo('seo_market_carbon_city'),
            'selectedMenu' => parent::MENU_MARKET_COO,

            'breadMenuList' => $this->breadMenuList,
            'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO],
            'selectedTab' => 'city',
        ];
        return view('web.market.carbon.city', $viewData);
    }

    public function carbonDistrict()
    {
        $viewData = [
            'seo' => $this->returnSeoInfo('seo_market_carbon_district'),
            'selectedMenu' => parent::MENU_MARKET_COO,

            'breadMenuList' => $this->breadMenuList,
            'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO],
            'selectedTab' => 'district',
        ];
        return view('web.market.carbon.district', $viewData);
    }

    public function carbonTown()
    {
        $viewData = [
            'seo' => $this->returnSeoInfo('seo_market_carbon_town'),
            'selectedMenu' => parent::MENU_MARKET_COO,

            'breadMenuList' => $this->breadMenuList,
            'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO],
            'selectedTab' => 'town',
        ];
        return view('web.market.carbon.town', $viewData);
    }

    public function carbonPark()
    {
        $viewData = [
            'seo' => $this->returnSeoInfo('seo_market_carbon_park'),
            'selectedMenu' => parent::MENU_MARKET_COO,

            'breadMenuList' => $this->breadMenuList,
            'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO],
            'selectedTab' => 'park',
        ];
        return view('web.market.carbon.park', $viewData);
    }

    public function servicerList()
    {

        $result = $this->getFilterServicer($this->request, parent::CODE_GET_SERVICE_LIST);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'pageCount' => $pageCount,
            ];
            return view('web.market.list.servicer', $viewData);
        } else {
            $filterList = $this->formData->getFilterList($this->request, parent::CODE_GET_SERVICE_FILTER_LIST);
            $domainList = $filterList ? $filterList['domainList'] : [];

            $techRouteList = $this->formData->getDomainTechRouteList($this->request, parent::CODE_GET_DOMAIN_TECHROUTE_LIST, $domainList);
            $techRouteList = $techRouteList ? $techRouteList['techRouteList'] : [];

            $viewData = [
                'seo' => $this->returnSeoInfo(parent::TAB_MENU_MARKET_COO_SERVICER),
                'selectedMenu' => parent::MENU_MARKET_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_MARKET_COO, parent::TAB_MENU_MARKET_COO_SERVICER],
                'dataList' => $dataList,

                'pageCount' => $pageCount,

                'areaList' => $this->cacheGetProvinceList(),
                'domainList' => $domainList,
                'techRouteList' => $techRouteList,
            ];
            return view('web.market.servicer', $viewData);
        }
    }
}
